package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.Message;
import cn.wolfcode.trip.base.domain.MessageContent;
import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.service.IMessageService;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.aspectj.bridge.IMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

@Controller
@RequestMapping("/travel")
public class TravelController {
    @Autowired
    private ITravelService travelService;
    @Autowired
    private IMessageService messageService;
    //待发布状态的游记管理
    @RequestMapping("/list")
    public String list(@ModelAttribute("qo")TravelQueryObject qo, Model model){
        //设置查询条件,查询的是审核的游记
        qo.setState(Travel.StATE_WAITING); //待发布状态
        qo.setIsPublic(true); //公开
        qo.setOrder("t.createTime desc"); //排序
        PageInfo pageInfo = travelService.query(qo);
        model.addAttribute("result", pageInfo);
        return "travel/list";
    }
    //也拒绝的游记管理
    @RequestMapping("/rejectTravel")
    public String rejectTravel(@ModelAttribute("qo")TravelQueryObject qo, Model model){
        //设置查询条件,查询的是审核的游记
        qo.setState(Travel.StATE_REJECT); //待发布状态
        qo.setIsPublic(true); //公开
        qo.setOrder("t.createTime desc"); //排序
        PageInfo pageInfo = travelService.query(qo);
        model.addAttribute("result", pageInfo);
        return "travel/rejectList";
    }
    //已发布游记管理
    @RequestMapping("/releaseList")
    public String releaseList(@ModelAttribute("qo")TravelQueryObject qo, Model model){
        //设置查询条件,查询的是审核的游记
        qo.setState(Travel.StATE_RELEASE); //已发布
        qo.setIsPublic(true); //公开
        qo.setOrder("t.releaseTime desc"); //排序
        PageInfo pageInfo = travelService.query(qo);
        model.addAttribute("result", pageInfo);
        return "travel/releaseList";
    }
    //修改游记的状态
    @RequestMapping("/changeList")
    @ResponseBody
    public Object changeList(Travel travel){
        JSONResult jsonResult = new JSONResult();
        try {
            //更新状态
            travelService.updateState(travel);
            //保存记录
            Message message = new Message();
            //设置指定的系统通知人
            User user = new User();
            user.setId(9L);
            //设置游记的id
            message.setUser(user);
            message.setCreatdate(new Date());
            User target = new User();
            target.setId(travel.getId());
            //表示的系统通知的类型
            message.setType(5);
            message.setTarget(target);
            messageService.insert(message,travel.getState());
        }catch (Exception e){
            e.printStackTrace();
            jsonResult.setMsg(e.getMessage());
        }
        return jsonResult;
    }
    //查询指定ID的游记内容
    @RequestMapping("/getContentByTravelId")
    @ResponseBody
    public Object changeList(Long id){
        return travelService.getContentByTravelId(id);
    }
    //查询相关的游记信息
    //在select下拉框
    @RequestMapping("/getRegionTravelByStrategyId")
    @ResponseBody
    public Object getRegionTravelByStrategyId(Long strategyId){
        return travelService.getRegionTravelByStrategyId(strategyId);
    }
}
